package com.tools.pojo.workflow.bo;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true) // 忽略未知字段
public class ExecutionPath {

    private List<PathNode> nodes = new ArrayList<>();

    private String startTime;

    private String endTime;

    private int totalNodes;

    private int executedNodes;

    private Boolean completed;

    public void addNode(PathNode node) {
        nodes.add(node);
        totalNodes = nodes.size();
        executedNodes = (int) nodes.stream()
                .filter(n -> n.getEndTime() != null)
                .count();
    }

    public PathNode getCurrentNode() {
        return nodes.stream()
                .filter(n -> n.getEndTime() == null)
                .findFirst()
                .orElse(null);
    }


    public boolean isCompleted() {
        return nodes.stream().allMatch(n -> n.getEndTime() != null);
    }

    // 确保 completed 字段与计算状态同步
    public Boolean getCompleted() {
        return isCompleted();
    }

    public void setCompleted(Boolean completed) {
        this.completed = completed;
        // 如果手动设置为 completed，确保所有节点都有结束时间
        if (Boolean.TRUE.equals(completed)) {
            for (PathNode node : nodes) {
                if (node.getEndTime() == null) {
                    node.setEndTime(LocalDateTime.now().toString());
                }
            }
        }
    }

}
